﻿using System;
using System.Collections.Generic;
using System.Text;
using AgilePlannerDT.Data;

namespace AgilePlannerDT.Persister.Impl
{
    public class AsyncWrapperPersister : AbstractAsyncPersister
    {
        SynchronousPersister persister;

        public AsyncWrapperPersister(SynchronousPersister persister)
        {
            this.persister = persister;
        }

        public override Project Project
        {
            get
            {
                return persister.Project;
            }
            set
            {
                persister.Project = value;
            }
        }

        public override bool IsConnected()
        {
            throw new NotImplementedException();
        }

        public override void Connect(string projectName)
        {
            Load(projectName);
        }

        public override void Disconnect()
        {
            throw new NotImplementedException();
        }

        public override void Load(string projectName)
        {
            try
            {
                LoadedProject(persister.Load(projectName));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void Load(string projectName, DateTime start, DateTime end)
        {
            AsynchronousException(new NotImplementedException());
        }

        public override void GetProjectNames()
        {
            try
            {
                GotProjectNames(persister.GetProjectNames());
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void CreateProject(string projectName)
        {
            try
            {
                CreatedProject(persister.CreateProject(projectName));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void CreateBacklog(int width, int height, int locationX, int locationY)
        {
            try
            {
                CreatedBacklog(persister.CreateBacklog(width, height, locationX, locationY));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void CreateIteration(string name, string description, int width, int height, int locationX, int locationY, float availableEffort, DateTime startDate, DateTime endDate)
        {
            try
            {
                CreatedIteration(persister.CreateIteration(name, description, width, height, locationX, locationY, availableEffort, startDate, endDate));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void CreateStoryCard(string name, string description, int width, int height, int locationX, int locationY, long parentid, float bestCaseEstimate, float mostlikelyEstimate, float worstCaseEstimate, float actualEffort, string status, string color, string cardOwner, float rotationAngle)
        {
            try
            {
                CreatedStoryCard(persister.CreateStoryCard(name, description, width, height, locationX, locationY, parentid, bestCaseEstimate, mostlikelyEstimate, worstCaseEstimate, actualEffort, status, color, cardOwner, rotationAngle,"",""));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void DeleteBacklog(long id)
        {
            try
            {
                DeletedBacklog((Backlog)persister.DeleteCard(id));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void DeleteIteration(long id)
        {
            try
            {
                DeletedIteration((Iteration)persister.DeleteCard(id));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void DeleteStoryCard(long id)
        {
            try
            {

                DeletedStoryCard((StoryCard)persister.DeleteCard(id));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void DeleteProject(string projectName)
        {
            try
            {
                DeletedProject(persister.DeleteProject(projectName));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UndeleteIteration(Iteration iteration)
        {
            try
            {
                UndeletedIteration((Iteration)persister.UndeleteCard(iteration));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UndeleteStoryCard(StoryCard storycard)
        {
            try
            {
                UndeletedStoryCard((StoryCard)persister.UndeleteCard(storycard));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void MoveStoryCardToNewParent(StoryCard sc, long newparentid, int locationX, int locationY, float rotationAngle)
        {
            try
            {
                MovedStoryCardToNewParent(
                    persister.MoveStoryCardToNewParent(
                    sc.ID, sc.Parent, newparentid, locationX, locationY, rotationAngle));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UpdateIteration(Iteration iteration)
        {
            try
            {
                UpdatedIteration((Iteration)persister.UpdateCard(iteration));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UpdateStoryCard(StoryCard storycard)
        {
            try
            {
                UpdatedStoryCard((StoryCard)persister.UpdateCard(storycard));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UpdateBacklog(Backlog backlog)
        {
            try
            {
                UpdatedBacklog((Backlog)persister.UpdateCard(backlog));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UpdateLegend(Legend legend)
        {
            try
            {
                UpdatedLegend((Legend)persister.UpdateLegend(legend));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void UpdateProject(Project project)
        {
            try
            {
                UpdatedProject((Project)persister.UpdateProject(project));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void ArrangeProject(Project project)
        {
            try
            {
                ArrangedProject(persister.ArrangeProject(project));
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void Login(string userName, string password)
        {
            try
            {
                AsynchronousException(new NotImplementedException());
            }
            catch (Exception e)
            {
                AsynchronousException(e);
            }
        }

        public override void MouseMove(MouseMove move)
        {
            
        }

        public override void LiveUpdate(LiveUpdate update)
        {
           
        }

        public override void MinimizeCard(long ID)
        {
            StoryCard sc = (StoryCard)((StoryCard)(Project.FindCard(ID))).Clone();
            bool isCardMinimized;
            if (sc.Height == CardConstants.DEFAULT_MINIMIZED_CARD_HEIGHT)
            {
                sc.Height = CardConstants.STORYCARD_DEFAULT_HEIGHT;
                sc.Width = CardConstants.STORYCARD_DEFAULT_WIDTH;
                isCardMinimized = true;

            }
            else
            {
                sc.Height = CardConstants.DEFAULT_MINIMIZED_CARD_HEIGHT;
                sc.Width = CardConstants.STORYCARD_DEFAULT_WIDTH;
                isCardMinimized = false;
            }
            this.UpdateStoryCard(sc);
            CardMinimized(ID,isCardMinimized);
        //    this.UpdateStoryCard(sc);
        }

        public override void SynchWithJazz(int MessageType, Dictionary<string, string> data)
        {
           
        }

        public override void AddTeamMember(TeamMember member)
        {
            persister.AddNewUser(member);

        }

        public override void RemoveTeamMember(TeamMember member)
        {
            persister.RemoveUser(member);
        }

        #region Dot Voting

        public override void StartVoting()
        {
            StartedVoting();
        }

        public override void StopVoting()
        {
            StoppedVoting();
        }

        public override void VoteCard(VoteDot dot)
        {
            VotedCard(dot);
        }

        public override void ClearVotes()
        {
            ClearedVotes();
        }

        #endregion

    }
}
